package StudentSysterm;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class StudentTestPlus {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        ArrayList<User> list=new ArrayList<>();

        loop:while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            System.out.println("--------------------------------------");
            String choose = sc.next();
            switch(choose){
                case "1":{
                    login(list);
                    break;
                }
                case "2":{
                    register(list);
                    break;
                }
                case "3":{
//                    resetpassword;
                    break;
                }
                case "4":{
                    System.out.println("退出");
                    break loop;
                }

            }
        }
    }

    private static void login(ArrayList<User> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = containsname(list,username);
        if(!flag){
            System.out.println("用户名"+username+"未注册，请先注册");
            return;
        }
        System.out.println("请输入密码");
        String password=sc.next();

        while (true) {
            System.out.println("请输入验证码");
            String rightcode=getcode();
            System.out.println(rightcode);
            String code=sc.next();
            if(code.equalsIgnoreCase(rightcode)){
                System.out.println("验证码正确");
                break;
            }
            else{
                System.out.println("验证码错误，请重新输入");
                continue;
            }
        }

        User user=new User(username,password,null,null);
        boolean flag1 = checkuserinfo(list, user);
        if(flag1){
            System.out.println("登录成功");
        }
        else{
            System.out.println("登录失败，用户名或密码错误");
            if(i==2)
        }

    }

    private static boolean checkuserinfo(ArrayList<User> list, User user) {
        for (int i = 0; i < list.size(); i++) {
            User rightuser=list.get(i);
            if(rightuser.getUserName().equals(user.getUserName())&&rightuser.getPassword().equals(user.getPassword())){
                return true;
            }
        }
        return false;
    }

    private static void register(ArrayList<User> list) {
        Scanner sc=new Scanner(System.in);
        String username;
        String password;
        String personid;
        String phone;
        //输入用户名
        while (true) {
            System.out.println("请输入用户名");
            username = sc.next();
            //检查用户名
            boolean flag1 = checkusername(username);
            if(!flag1){
                System.out.println("用户名格式不正确，请重新输入");
                continue;
            }
            //检验唯一性
            boolean flag2 = containsname(list, username);
            if(flag2){
                System.out.println("用户名"+username+"重复，请重新输入");
                continue;
            }
            System.out.println("用户名"+username+"可用");
            break;
        }
        //输入密码
        while (true) {
            System.out.println("请输入密码");
            password=sc.next();
            System.out.println("请再次输入密码");
            String againpassword=sc.next();
            //两次密码必须一致
            if(!password.equals(againpassword)){
                System.out.println("两次密码输入不一致，请重新输入");
            }
            else{
                System.out.println("两次密码输入一致");
                break;
            }

        }
        //输入身份证
        while (true) {
            System.out.println("请输入身份证");
            personid=sc.next();
            //检查身份证
            boolean flag3 = checkpersonid(personid);
            if(flag3){
                System.out.println("身份证号码符合要求");
                break;
            }
            else{
                System.out.println("身份证号码不符合要求");
            }
        }
        //输入手机号
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            //检查手机号
            boolean flag4 = checkphone(phone);
            if(flag4){
                System.out.println("手机号码符合标准");
                break;
            }
            else{
                System.out.println("手机号码不符合标准，请重新输入");
                continue;
            }
        }
        //添加信息到对象
        User user=new User(username,password,personid,phone);
        //把对象添加到集合
        list.add(user);
        System.out.println("注册成功");
        //遍历集合
        printlist(list);
    }

    private static void printlist(ArrayList<User> list) {
        for (int i = 0; i < list.size(); i++) {
            User user=list.get(i);
            System.out.println(user.getUserName()+","+user.getPassword()+","+user.getPersonid()+","+user.getPhone());
        }
    }

    private static boolean checkphone(String phone) {
        //开头不能为0
        if(phone.startsWith("0")){
            return false;
        }
        //长度11位
        if(phone.length()!=11){
            return false;
        }
        //全数字
        for (int i = 0; i < phone.length(); i++) {
            char c = phone.charAt(i);
            if(!(c>='0' && c<='9')){
                return false;
            }
        }
        return true;
    }

    private static boolean checkpersonid(String personid) {
        //首字符不能为0
        char f = personid.charAt(0);
        if(f=='0'){
            return false;
        }
        //长度为18位
        if(personid.length()!=18){
            return false;
        }
        //前17位必须是数字
        for (int i = 0; i < 17; i++) {
            char c = personid.charAt(i);
            if(!(c>='0' && c<='9')){
                return false;
            }
        }
        //最后一位是数字或'x'和'X'
        char end = personid.charAt(17);
        if((end>='0' && end<='9') || (end=='x') || (end=='X')){
            return true;
        }
        else{
            return false;
        }

    }

    private static boolean checkusername(String username) {
        //长度3-15位
        if(username.length()<3 || username.length()>15){
            System.out.println("用户名长度不符合标准，请重新输入用户名");
            return false;
        }
        //必须字母加数字
        //遍历
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            //判断
            if(!((c>='a' && c<='z')||(c>='A' && c<='Z')||(c>='0' && c<='9'))){
                System.out.println("用户名格式不符合标准，请重新输入用户名");
                return false;
            }
        }
        //不能纯数字（统计数字的数量必须小于字符串长度）
        int count=0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if((c>'a'&&c<'z')||(c>'A'&&c<'Z')){
                count++;
                //只要有一个字母就可以
                break;
            }
        }
            return count>0;

    }

    public static boolean containsname(ArrayList<User> list,String username){
        for (int i = 0; i < list.size(); i++) {
            String rightusername = list.get(i).getUserName();
            if(rightusername.equals(username)){
                return true;
            }
        }
        return false;
    }

    //生成验证码
    public static String getcode(){
        ArrayList<Character> list=new ArrayList<>();
        for (int i = 0; i <26 ; i++) {
            list.add((char) ('a'+ i));
            list.add((char) ('A'+ i));
        }
        Random r=new Random();
        StringBuilder sb=new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index=r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int num=r.nextInt(10);
        sb.append(num);

        char[] chars = sb.toString().toCharArray();
        int randomindex = r.nextInt(chars.length-1);
        char temp=chars[randomindex];
        chars[randomindex]=chars[chars.length-1];
        chars[chars.length-1]=temp;
        return new String(chars);
    }
}
